package class07;

/**
 * @description: https://leetcode.com/problems/balanced-binary-tree/
 * @time: 2021/7/17 9:45
 * @author: lhy
 */
public class BalancedBinaryTree {
    public static class Info{
        int height;
        boolean isBalanced;

        public Info(int height, boolean isBalanced) {
            this.height = height;
            this.isBalanced = isBalanced;
        }
    }
    public boolean isBalanced(TreeNode root) {
        return process(root).isBalanced;
    }
    public Info process(TreeNode root){
        if (root == null){
            return new Info(0,true);
        }
        Info leftInfo = process(root.left);
        Info rightInfo = process(root.right);
        int height = Math.max(leftInfo.height,rightInfo.height) + 1;
        boolean isBalanced = leftInfo.isBalanced && rightInfo.isBalanced && Math.abs(leftInfo.height - rightInfo.height) < 2;
        return new Info(height,isBalanced);
    }
}
